package cn.unuuc.cwl.common.exception;

import cn.unuuc.cwl.common.enums.ResStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.http.HttpStatus;

/**
 * 自定义服务异常
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class ServiceException extends RuntimeException {
    private Integer code;
    private String message;
    private String data;

    public ServiceException(String message) {
        super(message);
        this.code = 400;
        this.message = message;
    }

    public ServiceException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public ServiceException(Integer code, String message, String data) {
        this(code, message);
        this.data = data;
    }

    public ServiceException(ResStatus resStatus) {
        super(resStatus.getMessage());
        this.code = resStatus.getCode();
        this.message = resStatus.getMessage();
    }

    public static ServiceException of(Integer code, String message) {
        return new ServiceException(code, message);
    }

    public static ServiceException of(HttpStatus code, String message) {
        return new ServiceException(code.value(), message);
    }

    public static ServiceException of(String message) {
        return new ServiceException(500, message);
    }
}
